<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li class="active"><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. egg.js">1. egg.js</a><ul><li><a href="#t11.1 目录结构">1.1 目录结构</a></li><li><a href="#t21.2 访问">1.2 访问</a></li></ul></li><li><a href="#t32. 初始化项目">2. 初始化项目</a></li><li><a href="#t43. 添加 npm scripts 到 package.json：">3. 添加 npm scripts 到 package.json：</a></li><li><a href="#t54. 跑通路由">4. 跑通路由</a><ul><li><a href="#t64.1 配置路由">4.1 配置路由</a></li><li><a href="#t74.2 编写控制器">4.2 编写控制器</a></li><li><a href="#t84.3 编写配置文件">4.3 编写配置文件</a></li></ul></li><li><a href="#t95. 静态文件中间件">5. 静态文件中间件</a></li><li><a href="#t106. 使用模板引擎">6. 使用模板引擎</a><ul><li><a href="#t116.1 安装依赖的插件">6.1 安装依赖的插件</a></li><li><a href="#t126.2 启用插件">6.2 启用插件</a></li><li><a href="#t136.3 配置模板">6.3 配置模板</a></li><li><a href="#t146.4 编写模板">6.4 编写模板</a></li><li><a href="#t156.5 编写控制器">6.5 编写控制器</a></li></ul></li><li><a href="#t167. 读取远程接口服务">7. 读取远程接口服务</a><ul><li><a href="#t177.1  添加配置">7.1  添加配置</a></li><li><a href="#t187.2  编写Service">7.2  编写Service</a></li><li><a href="#t197.3  编写控制层">7.3  编写控制层</a></li></ul></li><li><a href="#t208. 扩展工具方法">8. 扩展工具方法</a></li><li><a href="#t219. 中间件">9. 中间件</a></li><li><a href="#t2210.运行环境">10.运行环境</a></li><li><a href="#t2311. 单元测试">11. 单元测试</a><ul><li><a href="#t2411.1 单元测试的优点">11.1 单元测试的优点</a></li><li><a href="#t2511.2 测试框架">11.2 测试框架</a></li><li><a href="#t2611.3 测试约定">11.3 测试约定</a><ul><li><a href="#t2711.3.1 测试目录结构">11.3.1 测试目录结构</a></li><li><a href="#t2811.3.2 测试运行工具">11.3.2 测试运行工具</a></li><li><a href="#t2911.3.3 mock">11.3.3 mock</a></li><li><a href="#t3011.3.4 app">11.3.4 app</a></li><li><a href="#t3111.3.5 ctx">11.3.5 ctx</a></li><li><a href="#t3211.3.6 测试执行顺序">11.3.6 测试执行顺序</a></li><li><a href="#t3311.3.7 异步测试">11.3.7 异步测试</a></li><li><a href="#t3411.3.8 Controller 测试">11.3.8 Controller 测试</a></li><li><a href="#t3511.3.9 post">11.3.9 post</a></li><li><a href="#t3611.3.10 service">11.3.10 service</a></li><li><a href="#t3711.3.11 Extend 测试">11.3.11 Extend 测试</a><ul><li><a href="#t3811.3.11.1 application">11.3.11.1 application</a></li><li><a href="#t3911.3.11.2 context">11.3.11.2 context</a></li><li><a href="#t4011.3.11.3 Request">11.3.11.3 Request</a></li><li><a href="#t4111.3.11.4 response">11.3.11.4 response</a></li><li><a href="#t4211.3.11.5 Helper">11.3.11.5 Helper</a></li></ul></li></ul></li></ul></li><li><a href="#t43参考">参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. egg.js">1. egg.js <a href="#t01. egg.js"> # </a></h2>
<ul>
<li><a href="https://eggjs.org/zh-cn/intro/">egg.js</a></li>
<li>&#x63D0;&#x4F9B;&#x57FA;&#x4E8E; Egg &#x5B9A;&#x5236;&#x4E0A;&#x5C42;&#x6846;&#x67B6;&#x7684;&#x80FD;&#x529B;</li>
<li>&#x9AD8;&#x5EA6;&#x53EF;&#x6269;&#x5C55;&#x7684;&#x63D2;&#x4EF6;&#x673A;&#x5236;</li>
<li>&#x5185;&#x7F6E;&#x591A;&#x8FDB;&#x7A0B;&#x7BA1;&#x7406;</li>
<li>&#x57FA;&#x4E8E; Koa &#x5F00;&#x53D1;&#xFF0C;&#x6027;&#x80FD;&#x4F18;&#x5F02;</li>
<li>&#x6846;&#x67B6;&#x7A33;&#x5B9A;&#xFF0C;&#x6D4B;&#x8BD5;&#x8986;&#x76D6;&#x7387;&#x9AD8;</li>
<li>&#x6E10;&#x8FDB;&#x5F0F;&#x5F00;&#x53D1;</li>
</ul>
<h3 id="t11.1 &#x76EE;&#x5F55;&#x7ED3;&#x6784;">1.1 &#x76EE;&#x5F55;&#x7ED3;&#x6784; <a href="#t11.1 &#x76EE;&#x5F55;&#x7ED3;&#x6784;"> # </a></h3>
<pre><code class="lang-js">&#x251C;&#x2500;&#x2500; package.json
&#x251C;&#x2500;&#x2500; app.js (app.js &#x548C; agent.js &#x7528;&#x4E8E;&#x81EA;&#x5B9A;&#x4E49;&#x542F;&#x52A8;&#x65F6;&#x7684;&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4F5C;)
&#x251C;&#x2500;&#x2500; agent.js (&#x53EF;&#x9009;)
&#x251C;&#x2500;&#x2500; app
|   &#x251C;&#x2500;&#x2500; router.js(&#x7528;&#x4E8E;&#x914D;&#x7F6E; URL &#x8DEF;&#x7531;&#x89C4;&#x5219;)
&#x2502;   &#x251C;&#x2500;&#x2500; controller(&#x7528;&#x4E8E;&#x89E3;&#x6790;&#x7528;&#x6237;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x5904;&#x7406;&#x540E;&#x8FD4;&#x56DE;&#x76F8;&#x5E94;&#x7684;&#x7ED3;&#x679C;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; home.js
&#x2502;   &#x251C;&#x2500;&#x2500; service (&#x7528;&#x4E8E;&#x7F16;&#x5199;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x5C42;&#xFF0C;&#x53EF;&#x9009;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; user.js
&#x2502;   &#x251C;&#x2500;&#x2500; middleware (&#x7528;&#x4E8E;&#x7F16;&#x5199;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x53EF;&#x9009;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; response_time.js
&#x2502;   &#x251C;&#x2500;&#x2500; schedule (&#x7528;&#x4E8E;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#xFF0C;&#x53EF;&#x9009;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; my_task.js
&#x2502;   &#x251C;&#x2500;&#x2500; public (&#x7528;&#x4E8E;&#x653E;&#x7F6E;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;&#x53EF;&#x9009;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; reset.css
&#x2502;   &#x251C;&#x2500;&#x2500; extend (&#x7528;&#x4E8E;&#x6846;&#x67B6;&#x7684;&#x6269;&#x5C55;&#xFF0C;&#x53EF;&#x9009;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; application.js app &#x5BF9;&#x8C61;&#x6307;&#x7684;&#x662F; Koa &#x7684;&#x5168;&#x5C40;&#x5E94;&#x7528;&#x5BF9;&#x8C61;&#xFF0C;&#x5168;&#x5C40;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x5728;&#x5E94;&#x7528;&#x542F;&#x52A8;&#x65F6;&#x88AB;&#x521B;&#x5EFA;&#x3002;
&#x2502;       &#x251C;&#x2500;&#x2500; context.js (Context &#x6307;&#x7684;&#x662F; Koa &#x7684;&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x8FD9;&#x662F; &#x8BF7;&#x6C42;&#x7EA7;&#x522B; &#x7684;&#x5BF9;&#x8C61;)
&#x2502;       &#x251C;&#x2500;&#x2500; request.js (Request &#x5BF9;&#x8C61;&#x548C; Koa &#x7684; Request &#x5BF9;&#x8C61;&#x76F8;&#x540C;&#xFF0C;&#x662F; &#x8BF7;&#x6C42;&#x7EA7;&#x522B; &#x7684;&#x5BF9;&#x8C61;)
&#x2502;       &#x251C;&#x2500;&#x2500; response.js (Response &#x5BF9;&#x8C61;&#x548C; Koa &#x7684; Response &#x5BF9;&#x8C61;&#x76F8;&#x540C;&#xFF0C;&#x662F; &#x8BF7;&#x6C42;&#x7EA7;&#x522B; &#x7684;&#x5BF9;&#x8C61;)
&#x2502;       &#x251C;&#x2500;&#x2500; helper.js (Helper &#x51FD;&#x6570;&#x7528;&#x6765;&#x63D0;&#x4F9B;&#x4E00;&#x4E9B;&#x5B9E;&#x7528;&#x7684; utility &#x51FD;&#x6570;)
&#x2502;   &#x251C;&#x2500;&#x2500; view (&#x7528;&#x4E8E;&#x653E;&#x7F6E;&#x6A21;&#x677F;&#x6587;&#x4EF6;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; home.tpl
&#x251C;&#x2500;&#x2500; |&#x2500;&#x2500; model (&#x7528;&#x4E8E;&#x653E;&#x7F6E;&#x9886;&#x57DF;&#x6A21;&#x578B;)
&#x2502;   |   &#x2514;&#x2500;&#x2500; home.tpl
&#x2502;   &#x2514;&#x2500;&#x2500; extend (&#x7528;&#x4E8E;&#x6846;&#x67B6;&#x7684;&#x6269;&#x5C55;)
&#x2502;       &#x251C;&#x2500;&#x2500; helper.js (&#x53EF;&#x9009;)
&#x2502;       &#x251C;&#x2500;&#x2500; request.js (&#x53EF;&#x9009;)
&#x2502;       &#x251C;&#x2500;&#x2500; response.js (&#x53EF;&#x9009;)
&#x2502;       &#x251C;&#x2500;&#x2500; context.js (&#x53EF;&#x9009;)
&#x2502;       &#x251C;&#x2500;&#x2500; application.js (&#x53EF;&#x9009;)
&#x2502;       &#x2514;&#x2500;&#x2500; agent.js (&#x53EF;&#x9009;)
&#x251C;&#x2500;&#x2500; config(&#x7528;&#x4E8E;&#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;)
|   &#x251C;&#x2500;&#x2500; plugin.js(&#x7528;&#x4E8E;&#x914D;&#x7F6E;&#x9700;&#x8981;&#x52A0;&#x8F7D;&#x7684;&#x63D2;&#x4EF6;)
|   &#x251C;&#x2500;&#x2500; config.default.js
&#x2502;   &#x251C;&#x2500;&#x2500; config.prod.js
|   &#x251C;&#x2500;&#x2500; config.test.js (&#x53EF;&#x9009;)
|   &#x251C;&#x2500;&#x2500; config.local.js (&#x53EF;&#x9009;)
|   &#x2514;&#x2500;&#x2500; config.unittest.js (&#x53EF;&#x9009;)
&#x2514;&#x2500;&#x2500; test(&#x7528;&#x4E8E;&#x5355;&#x5143;&#x6D4B;&#x8BD5;)
    &#x251C;&#x2500;&#x2500; middleware
    |   &#x2514;&#x2500;&#x2500; response_time.test.js
    &#x2514;&#x2500;&#x2500; controller
        &#x2514;&#x2500;&#x2500; home.test.js
</code></pre>
<h3 id="t21.2 &#x8BBF;&#x95EE;">1.2 &#x8BBF;&#x95EE; <a href="#t21.2 &#x8BBF;&#x95EE;"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x6587;&#x4EF6;</th>
<th style="text-align:left">app</th>
<th style="text-align:left">ctx</th>
<th style="text-align:left">service</th>
<th style="text-align:left">config</th>
<th style="text-align:left">logger</th>
<th style="text-align:left">helper</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Controller</td>
<td style="text-align:left">this.app</td>
<td style="text-align:left">this.ctx</td>
<td style="text-align:left">this.service</td>
<td style="text-align:left">this.config</td>
<td style="text-align:left">this.logger</td>
<td style="text-align:left">this.app.helper</td>
</tr>
<tr>
<td style="text-align:left">Service</td>
<td style="text-align:left">this.app</td>
<td style="text-align:left">this.ctx</td>
<td style="text-align:left">this.service</td>
<td style="text-align:left">this.config</td>
<td style="text-align:left">this.logger</td>
<td style="text-align:left">this.app.helper</td>
</tr>
</tbody>
</table>
<blockquote>
<p>ctx.helper </p>
</blockquote>
<h2 id="t32. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;">2. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE; <a href="#t32. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;"> # </a></h2>
<pre><code class="lang-js">mkdir egg-news
cd egg-news
npm init -y
cnpm i egg --save
cnpm i egg-bin --save-dev
</code></pre>
<h2 id="t43. &#x6DFB;&#x52A0; npm scripts &#x5230; package.json&#xFF1A;">3. &#x6DFB;&#x52A0; npm scripts &#x5230; package.json&#xFF1A; <a href="#t43. &#x6DFB;&#x52A0; npm scripts &#x5230; package.json&#xFF1A;"> # </a></h2>
<pre><code class="lang-json">&quot;scripts&quot;: {
    &quot;dev&quot;: &quot;egg-bin dev&quot;
}
</code></pre>
<h2 id="t54. &#x8DD1;&#x901A;&#x8DEF;&#x7531;">4. &#x8DD1;&#x901A;&#x8DEF;&#x7531; <a href="#t54. &#x8DD1;&#x901A;&#x8DEF;&#x7531;"> # </a></h2>
<pre><code class="lang-js">&#x251C;&#x2500;app
&#x2502;  &#x2502;&#x2500;router.js
&#x2502;  &#x251C;&#x2500;controller
&#x2502;  &#x2502;      news.js
&#x251C;&#x2500;config
&#x2502;      config.default.js
|&#x2500;package.json
</code></pre>
<h3 id="t64.1 &#x914D;&#x7F6E;&#x8DEF;&#x7531;">4.1 &#x914D;&#x7F6E;&#x8DEF;&#x7531; <a href="#t64.1 &#x914D;&#x7F6E;&#x8DEF;&#x7531;"> # </a></h3>
<p>app/router.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-params">app</span> =&gt;</span> {
    <span class="hljs-keyword">const</span> { router, controller } = app;
    router.get(<span class="hljs-string">&apos;/news&apos;</span>, controller.news.index);
}
</code></pre>
<h3 id="t74.2 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668;">4.2 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668; <a href="#t74.2 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668;"> # </a></h3>
<p>app\controller\news.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { Controller } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewsController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span> </span>{
    <span class="hljs-keyword">async</span> index() {
        <span class="hljs-keyword">this</span>.ctx.body = <span class="hljs-string">&apos;hello world&apos;</span>;
    }
}
<span class="hljs-built_in">module</span>.exports = NewsController;
</code></pre>
<h3 id="t84.3 &#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;">4.3 &#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6; <a href="#t84.3 &#x7F16;&#x5199;&#x914D;&#x7F6E;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">exports.keys = <span class="hljs-string">&apos;zfpx&apos;</span>;
</code></pre>
<h2 id="t95. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x4E2D;&#x95F4;&#x4EF6;">5. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x4E2D;&#x95F4;&#x4EF6; <a href="#t95. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x4E2D;&#x95F4;&#x4EF6;"> # </a></h2>
<ul>
<li>Egg &#x5185;&#x7F6E;&#x4E86; static &#x63D2;&#x4EF6;</li>
<li>static &#x63D2;&#x4EF6;&#x9ED8;&#x8BA4;&#x6620;&#x5C04; /public/<em> -&gt; app/public/</em> &#x76EE;&#x5F55;</li>
<li>&#x628A;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x90FD;&#x653E;&#x5230; app/public &#x76EE;&#x5F55;&#x5373;&#x53EF;</li>
<li><a href="https://v3.bootcss.com">bootcss</a></li>
</ul>
<h2 id="t106. &#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;">6. &#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE; <a href="#t106. &#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"> # </a></h2>
<pre><code class="lang-js">&#x251C;&#x2500;app
&#x2502;  &#x2502;&#x2500;router.js
&#x2502;  &#x251C;&#x2500;controller
&#x2502;  &#x2502;      news.js   
&#x2502;  &#x251C;&#x2500;public
&#x2502;  &#x2502;  &#x251C;&#x2500;css
&#x2502;  &#x2502;  &#x2502;      bootstrap.css  
&#x2502;  &#x2502;  &#x2514;&#x2500;js
&#x2502;  &#x2502;          bootstrap.js         
&#x2502;  &#x2514;&#x2500;view
&#x2502;          news.ejs       
&#x251C;&#x2500;config
&#x2502;   config.default.js
&#x2502;   plugin.js
</code></pre>
<h3 id="t116.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x7684;&#x63D2;&#x4EF6;">6.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x7684;&#x63D2;&#x4EF6; <a href="#t116.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x7684;&#x63D2;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">cnpm install egg-view-nunjucks --save
</code></pre>
<h3 id="t126.2 &#x542F;&#x7528;&#x63D2;&#x4EF6;">6.2 &#x542F;&#x7528;&#x63D2;&#x4EF6; <a href="#t126.2 &#x542F;&#x7528;&#x63D2;&#x4EF6;"> # </a></h3>
<p>{ROOT}\config\plugin.js</p>
<pre><code class="lang-js">exports.nunjucks = {
    <span class="hljs-attr">enable</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">package</span>: <span class="hljs-string">&apos;egg-view-nunjucks&apos;</span>
}
</code></pre>
<h3 id="t136.3 &#x914D;&#x7F6E;&#x6A21;&#x677F;">6.3 &#x914D;&#x7F6E;&#x6A21;&#x677F; <a href="#t136.3 &#x914D;&#x7F6E;&#x6A21;&#x677F;"> # </a></h3>
<p>{ROOT}\config\config.default.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports=<span class="hljs-function"><span class="hljs-params">app</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> config={};

    config.keys=<span class="hljs-string">&apos;zfpx&apos;</span>;

    config.view={
        <span class="hljs-attr">defaultExtension</span>: <span class="hljs-string">&apos;.html&apos;</span>,
        <span class="hljs-attr">defaultViewEngine</span>: <span class="hljs-string">&apos;nunjucks&apos;</span>,
        <span class="hljs-attr">mapping</span>: {
            <span class="hljs-string">&apos;.html&apos;</span>:<span class="hljs-string">&apos;nunjucks&apos;</span>
        }
    }
    <span class="hljs-keyword">return</span> config;
}
</code></pre>
<h3 id="t146.4 &#x7F16;&#x5199;&#x6A21;&#x677F;">6.4 &#x7F16;&#x5199;&#x6A21;&#x677F; <a href="#t146.4 &#x7F16;&#x5199;&#x6A21;&#x677F;"> # </a></h3>
<pre><code class="lang-js">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;/public/bootstrap/css/bootstrap.css&quot;&gt;
    &lt;title&gt;{{title}}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row justify-content-center mt-4&quot;&gt;
            &lt;div class=&quot;col-auto&quot;&gt;
                   {% for news in list%}
                        &lt;div class=&quot;card border-primary&quot;&gt;
                            &lt;img class=&quot;card-img-top&quot; src=&quot;{{news.image}}&quot; style=&quot;width:100%;&quot; /&gt;
                            &lt;div class=&quot;card-body&quot;&gt;
                                &lt;p class=&quot;card-text&quot;&gt;
                                  &lt;a href=&quot;{{news.url}}&quot;&gt;{{news.title}}&lt;/a&gt;
                                &lt;/p&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    {% endfor %}
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h3 id="t156.5 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668;">6.5 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668; <a href="#t156.5 &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5668;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> {Controller}=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewsController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{
    <span class="hljs-keyword">async</span> index() {
        <span class="hljs-keyword">const</span> {ctx}=<span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> list=[
            {
                <span class="hljs-attr">id</span>: <span class="hljs-string">&apos;45154322_0&apos;</span>,
                <span class="hljs-attr">title</span>: <span class="hljs-string">&apos;&#x4E16;&#x754C;&#x9996;&#x5BCC;&#x65E9;&#x665A;&#x662F;&#x8FD9;&#x4E2A;&#x4EBA;&#xFF0C;&#x5750;&#x62E5;7&#x5BB6;&#x72EC;&#x89D2;&#x517D;&#x516C;&#x53F8;&#xFF0C;&#x4F30;&#x503C;&#x7834;&#x6570;&#x4E07;&#xFF01;&apos;</span>,
                <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;http://tech.ifeng.com/a/20180904/45154322_0.shtml&apos;</span>,
                <span class="hljs-attr">image</span>:<span class="hljs-string">&apos;http://p0.ifengimg.com/pmop/2018/0905/CFFF918B94D561D2A61FB434ADA81589E8972025_size41_w640_h479.jpeg&apos;</span>
            },
            {
                <span class="hljs-attr">id</span>: <span class="hljs-string">&apos;16491630_0&apos;</span>,
                <span class="hljs-attr">title</span>: <span class="hljs-string">&apos;&#x652F;&#x4ED8;&#x5B9D;&#x4EEC;&#x6765;&#x4E86;&#xFF01;&#x5C06;&#x6765;&#x4EBA;&#x6C11;&#x5E01;&#x4F1A;&#x6D88;&#x5931;&#x5417;&#xFF1F;&apos;</span>,
                <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;http://finance.ifeng.com/a/20180907/16491630_0.shtml&apos;</span>,
                <span class="hljs-attr">image</span>:<span class="hljs-string">&apos;http://p0.ifengimg.com/pmop/2018/0907/2AF684C2EC49B7E3C17FCB13D6DEEF08401D4567_size27_w530_h369.jpeg&apos;</span>
            },
            {
                <span class="hljs-attr">id</span>: <span class="hljs-string">&apos;2451982&apos;</span>,
                <span class="hljs-attr">title</span>: <span class="hljs-string">&apos;&#x300A;&#x798F;&#x5E03;&#x65AF;&#x300B;&#x4E13;&#x8BBF;&#x8D1D;&#x7D22;&#x65AF;&#xFF1A;&#x65E0;&#x4E1A;&#x52A1;&#x8FB9;&#x754C;&#x7684;&#x4E9A;&#x9A6C;&#x900A; &#x4EE4;&#x5BF9;&#x624B;&#x751F;&#x754F;&#x7684;CEO&apos;</span>,
                <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;https://www.jiemian.com/article/2451982.html&apos;</span>,
                <span class="hljs-attr">image</span>:<span class="hljs-string">&apos;https://img1.jiemian.com/101/original/20180907/153628523948814900_a580x330.jpg&apos;</span>
            }
        ];
        <span class="hljs-keyword">await</span> ctx.render(<span class="hljs-string">&apos;index&apos;</span>,{list});
    }
}
<span class="hljs-built_in">module</span>.exports=NewsController;
</code></pre>
<h2 id="t167. &#x8BFB;&#x53D6;&#x8FDC;&#x7A0B;&#x63A5;&#x53E3;&#x670D;&#x52A1;">7. &#x8BFB;&#x53D6;&#x8FDC;&#x7A0B;&#x63A5;&#x53E3;&#x670D;&#x52A1; <a href="#t167. &#x8BFB;&#x53D6;&#x8FDC;&#x7A0B;&#x63A5;&#x53E3;&#x670D;&#x52A1;"> # </a></h2>
<p>&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;Controller &#x4E00;&#x822C;&#x4E0D;&#x4F1A;&#x81EA;&#x5DF1;&#x4EA7;&#x51FA;&#x6570;&#x636E;&#xFF0C;&#x4E5F;&#x4E0D;&#x4F1A;&#x5305;&#x542B;&#x590D;&#x6742;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x590D;&#x6742;&#x7684;&#x8FC7;&#x7A0B;&#x5E94;&#x62BD;&#x8C61;&#x4E3A;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x5C42; Service&#x3002;</p>
<h3 id="t177.1  &#x6DFB;&#x52A0;&#x914D;&#x7F6E;">7.1  &#x6DFB;&#x52A0;&#x914D;&#x7F6E; <a href="#t177.1  &#x6DFB;&#x52A0;&#x914D;&#x7F6E;"> # </a></h3>
<p>config.default.js</p>
<pre><code class="lang-js">config.news={
        <span class="hljs-attr">newsListUrl</span>: <span class="hljs-string">&apos;https://www.easy-mock.com/mock/5b923eb2321f1076a4fc13f4/api/news&apos;</span>,
}
</code></pre>
<h3 id="t187.2  &#x7F16;&#x5199;Service">7.2  &#x7F16;&#x5199;Service <a href="#t187.2  &#x7F16;&#x5199;Service"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> {Service}=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewsService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Service</span> </span>{
    <span class="hljs-keyword">async</span> list(pageNum,pageSize) {
        <span class="hljs-keyword">const</span> {ctx}=<span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> {newsListUrl}=<span class="hljs-keyword">this</span>.config.news;
        <span class="hljs-keyword">const</span> result=<span class="hljs-keyword">await</span> ctx.curl(newsListUrl,{
            <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;GET&apos;</span>,
            <span class="hljs-attr">data</span>: {
                pageNum,pageSize
            },
            <span class="hljs-attr">dataType</span>:<span class="hljs-string">&apos;json&apos;</span>
        });
        <span class="hljs-keyword">return</span> result.data.data;
    }
}
<span class="hljs-built_in">module</span>.exports=NewsService;
</code></pre>
<h3 id="t197.3  &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5C42;">7.3  &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5C42; <a href="#t197.3  &#x7F16;&#x5199;&#x63A7;&#x5236;&#x5C42;"> # </a></h3>
<p>app/controller/news.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> {Controller}=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewsController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{
    <span class="hljs-keyword">async</span> index() {
        <span class="hljs-keyword">const</span> {ctx,service}=<span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">let</span> {pageNum=<span class="hljs-number">1</span>,pageSize=<span class="hljs-keyword">this</span>.config.news.pageSize}=ctx.query;
        <span class="hljs-keyword">const</span> list=<span class="hljs-keyword">await</span> service.news.list(pageNum,pageSize);
        <span class="hljs-keyword">await</span> ctx.render(<span class="hljs-string">&apos;index&apos;</span>,{list});
    }
}
<span class="hljs-built_in">module</span>.exports=NewsController;
</code></pre>
<h2 id="t208. &#x6269;&#x5C55;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;">8. &#x6269;&#x5C55;&#x5DE5;&#x5177;&#x65B9;&#x6CD5; <a href="#t208. &#x6269;&#x5C55;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;"> # </a></h2>
<ul>
<li>&#x6846;&#x67B6;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x5FEB;&#x901F;&#x6269;&#x5C55;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EA;&#x9700;&#x5728;<code>app/extend</code>&#x76EE;&#x5F55;&#x4E0B;&#x63D0;&#x4F9B;&#x6269;&#x5C55;&#x811A;&#x672C;&#x5373;&#x53EF;</li>
<li>Helper &#x51FD;&#x6570;&#x7528;&#x6765;&#x63D0;&#x4F9B;&#x4E00;&#x4E9B;&#x5B9E;&#x7528;&#x7684; <code>utility</code> &#x51FD;&#x6570;&#x3002;</li>
<li>&#x8BBF;&#x95EE;&#x65B9;&#x5F0F; &#x901A;&#x8FC7; <code>ctx.helper</code> &#x8BBF;&#x95EE;&#x5230; helper &#x5BF9;&#x8C61;</li>
</ul>
<p>app\extend\helper.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moment=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;moment&apos;</span>);
moment.locale(<span class="hljs-string">&apos;zh-cn&apos;</span>);
exports.fromNow=<span class="hljs-function"><span class="hljs-params">dateTime</span> =&gt;</span> moment(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(dateTime)).fromNow();
</code></pre>
<p>news.js</p>
<pre><code class="lang-js">list.forEach(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> {
            item.createAt=ctx.helper.fromNow(item.createAt);
            <span class="hljs-keyword">return</span> item;
});
</code></pre>
<p>index.html</p>
<pre><code class="lang-js">&#x65F6;&#x95F4;: {{helper.fromNow(news.createAt)}}
</code></pre>
<h2 id="t219. &#x4E2D;&#x95F4;&#x4EF6;">9. &#x4E2D;&#x95F4;&#x4EF6; <a href="#t219. &#x4E2D;&#x95F4;&#x4EF6;"> # </a></h2>
<p>app/middleware/robot.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports=<span class="hljs-function">(<span class="hljs-params">options,app</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ctx,next</span>) </span>{
        <span class="hljs-keyword">const</span> source=ctx.get(<span class="hljs-string">&apos;user-agent&apos;</span>)||<span class="hljs-string">&apos;&apos;</span>;
        <span class="hljs-keyword">const</span> matched=options.ua.some(<span class="hljs-function"><span class="hljs-params">ua</span> =&gt;</span> ua.test(source));
        <span class="hljs-keyword">if</span> (matched) {
            ctx.status=<span class="hljs-number">403</span>;
            ctx.body=<span class="hljs-string">&apos;&#x4F60;&#x6CA1;&#x6709;&#x8BBF;&#x95EE;&#x6743;&#x9650;&apos;</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">await</span> next();
        }
    }
}
</code></pre>
<p>config.default.js</p>
<pre><code class="lang-js">    config.middleware=[
        <span class="hljs-string">&apos;robot&apos;</span>
    ]
    config.robot={
        <span class="hljs-attr">ua</span>: [
            <span class="hljs-regexp">/Chrome/</span>
        ]
    }
</code></pre>
<h2 id="t2210.&#x8FD0;&#x884C;&#x73AF;&#x5883;">10.&#x8FD0;&#x884C;&#x73AF;&#x5883; <a href="#t2210.&#x8FD0;&#x884C;&#x73AF;&#x5883;"> # </a></h2>
<p>&#x6846;&#x67B6;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x6307;&#x5B9A;&#x8FD0;&#x884C;&#x73AF;&#x5883;&#xFF1A;</p>
<ul>
<li>&#x901A;&#x8FC7; <code>config/env</code> &#x6587;&#x4EF6;&#x6307;&#x5B9A;&#xFF0C;&#x8BE5;&#x6587;&#x4EF6;&#x7684;&#x5185;&#x5BB9;&#x5C31;&#x662F;&#x8FD0;&#x884C;&#x73AF;&#x5883;&#xFF0C;&#x5982; prod&#x3002;</li>
<li>&#x901A;&#x8FC7; <code>EGG_SERVER_ENV</code> &#x73AF;&#x5883;&#x53D8;&#x91CF;&#x6307;&#x5B9A;&#x3002;</li>
<li>&#x6846;&#x67B6;&#x63D0;&#x4F9B;&#x4E86;&#x53D8;&#x91CF; <code>app.config.env</code> &#x6765;&#x8868;&#x793A;&#x5E94;&#x7528;&#x5F53;&#x524D;&#x7684;&#x8FD0;&#x884C;&#x73AF;&#x5883;&#x3002;</li>
<li>&#x652F;&#x6301;&#x6309;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x52A0;&#x8F7D;&#x4E0D;&#x540C;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x5982; <code>config.local.js</code>&#xFF0C; <code>config.prod.js</code> &#x7B49;&#x7B49;</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">EGG_SERVER_ENV</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">local</td>
<td style="text-align:left">&#x672C;&#x5730;&#x5F00;&#x53D1;&#x73AF;&#x5883;</td>
</tr>
<tr>
<td style="text-align:left">prod</td>
<td style="text-align:left">&#x751F;&#x4EA7;&#x73AF;&#x5883;</td>
</tr>
</tbody>
</table>
<pre><code class="lang-js">npm install  cross-env --save-dev
</code></pre>
<pre><code class="lang-js"><span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;start&quot;</span>: <span class="hljs-string">&quot;cross-env EGG_SERVER_ENV=local  egg-bin dev&quot;</span>,
    <span class="hljs-string">&quot;debug&quot;</span>: <span class="hljs-string">&quot;egg-bin debug&quot;</span>
}
</code></pre>
<h2 id="t2311. &#x5355;&#x5143;&#x6D4B;&#x8BD5;">11. &#x5355;&#x5143;&#x6D4B;&#x8BD5; <a href="#t2311. &#x5355;&#x5143;&#x6D4B;&#x8BD5;"> # </a></h2>
<h3 id="t2411.1 &#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x4F18;&#x70B9;">11.1 &#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x4F18;&#x70B9; <a href="#t2411.1 &#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x4F18;&#x70B9;"> # </a></h3>
<ul>
<li>&#x4EE3;&#x7801;&#x8D28;&#x91CF;&#x6301;&#x7EED;&#x6709;&#x4FDD;&#x969C;</li>
<li>&#x91CD;&#x6784;&#x6B63;&#x786E;&#x6027;&#x4FDD;&#x969C;</li>
<li>&#x589E;&#x5F3A;&#x81EA;&#x4FE1;&#x5FC3;</li>
<li>&#x81EA;&#x52A8;&#x5316;&#x8FD0;&#x884C;</li>
</ul>
<h3 id="t2511.2 &#x6D4B;&#x8BD5;&#x6846;&#x67B6;">11.2 &#x6D4B;&#x8BD5;&#x6846;&#x67B6; <a href="#t2511.2 &#x6D4B;&#x8BD5;&#x6846;&#x67B6;"> # </a></h3>
<ul>
<li><a href="https://mochajs.org/">mochajs</a></li>
<li><a href="https://github.com/power-assert-js/power-assert">power-assert</a></li>
</ul>
<h3 id="t2611.3 &#x6D4B;&#x8BD5;&#x7EA6;&#x5B9A;">11.3 &#x6D4B;&#x8BD5;&#x7EA6;&#x5B9A; <a href="#t2611.3 &#x6D4B;&#x8BD5;&#x7EA6;&#x5B9A;"> # </a></h3>
<h4 id="t2711.3.1 &#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x7ED3;&#x6784;">11.3.1 &#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x7ED3;&#x6784; <a href="#t2711.3.1 &#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"> # </a></h4>
<ul>
<li>&#x6211;&#x4EEC;&#x7EA6;&#x5B9A; <code>test</code> &#x76EE;&#x5F55;&#x4E3A;&#x5B58;&#x653E;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x7684;&#x76EE;&#x5F55;&#xFF0C;&#x6D4B;&#x8BD5;&#x6240;&#x4F7F;&#x7528;&#x5230;&#x7684; <code>fixtures</code> &#x548C;&#x76F8;&#x5173;&#x8F85;&#x52A9;&#x811A;&#x672C;&#x90FD;&#x5E94;&#x8BE5;&#x653E;&#x5728;&#x6B64;&#x76EE;&#x5F55;&#x4E0B;&#x3002;</li>
<li>&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x6587;&#x4EF6;&#x7EDF;&#x4E00;&#x6309; ${filename}.test.js &#x547D;&#x540D;&#xFF0C;&#x5FC5;&#x987B;&#x4EE5; .test.js &#x4F5C;&#x4E3A;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x3002;
&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x7684;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x793A;&#x4F8B;&#xFF1A;<pre><code class="lang-js">test
&#x251C;&#x2500;&#x2500; controller
&#x2502;   &#x2514;&#x2500;&#x2500; home.test.js
&#x2514;&#x2500;&#x2500; service
  &#x2514;&#x2500;&#x2500; user.test.js
</code></pre>
</li>
</ul>
<h4 id="t2811.3.2 &#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x5DE5;&#x5177;">11.3.2 &#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x5DE5;&#x5177; <a href="#t2811.3.2 &#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x5DE5;&#x5177;"> # </a></h4>
<p>&#x7EDF;&#x4E00;&#x4F7F;&#x7528; egg-bin &#x6765;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#xFF0C; &#x81EA;&#x52A8;&#x5C06;&#x5185;&#x7F6E;&#x7684; Mocha&#x3001;co-mocha&#x3001;power-assert&#xFF0C;nyc &#x7B49;&#x6A21;&#x5757;&#x7EC4;&#x5408;&#x5F15;&#x5165;&#x5230;&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x4E2D;&#xFF0C; &#x8BA9;&#x6211;&#x4EEC;&#x805A;&#x7126;&#x7CBE;&#x529B;&#x5728;&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x4E0A;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7EA0;&#x7ED3;&#x9009;&#x62E9;&#x90A3;&#x4E9B;&#x6D4B;&#x8BD5;&#x5468;&#x8FB9;&#x5DE5;&#x5177;&#x548C;&#x6A21;&#x5757;&#x3002;</p>
<pre><code class="lang-json">  &quot;scripts&quot;: {
    &quot;test&quot;: &quot;egg-bin test&quot;,
    &quot;cov&quot;: &quot;egg-bin cov&quot;
  }
</code></pre>
<h4 id="t2911.3.3 mock">11.3.3 mock <a href="#t2911.3.3 mock"> # </a></h4>
<p>&#x6B63;&#x5E38;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x5B8C;&#x6574;&#x624B;&#x5199;&#x4E00;&#x4E2A; app &#x521B;&#x5EFA;&#x548C;&#x542F;&#x52A8;&#x4EE3;&#x7801;&#xFF0C;&#x8FD8;&#x662F;&#x9700;&#x8981;&#x5199;&#x4E00;&#x6BB5;&#x521D;&#x59CB;&#x5316;&#x811A;&#x672C;&#x7684;&#xFF0C; &#x5E76;&#x4E14;&#x8FD8;&#x9700;&#x8981;&#x5728;&#x6D4B;&#x8BD5;&#x8DD1;&#x5B8C;&#x4E4B;&#x540E;&#x505A;&#x4E00;&#x4E9B;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;&#xFF0C;&#x5982;&#x5220;&#x9664;&#x4E34;&#x65F6;&#x6587;&#x4EF6;&#xFF0C;&#x9500;&#x6BC1; app&#x3002;</p>
<p>&#x5E38;&#x5E38;&#x8FD8;&#x6709;&#x6A21;&#x62DF;&#x5404;&#x79CD;&#x7F51;&#x7EDC;&#x5F02;&#x5E38;&#xFF0C;&#x670D;&#x52A1;&#x8BBF;&#x95EE;&#x5F02;&#x5E38;&#x7B49;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5355;&#x72EC;&#x4E3A;&#x6846;&#x67B6;&#x62BD;&#x53D6;&#x4E86;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5; mock &#x8F85;&#x52A9;&#x6A21;&#x5757;&#xFF1A;egg-mock&#xFF0C; &#x6709;&#x4E86;&#x5B83;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x975E;&#x5E38;&#x5FEB;&#x901F;&#x5730;&#x7F16;&#x5199;&#x4E00;&#x4E2A; app &#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x4E14;&#x8FD8;&#x80FD;&#x5FEB;&#x901F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; ctx &#x6765;&#x6D4B;&#x8BD5;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C; Service &#x7B49;&#x3002;</p>
<h4 id="t3011.3.4 app">11.3.4 app <a href="#t3011.3.4 app"> # </a></h4>
<p>&#x5728;&#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x8981;&#x521B;&#x5EFA;&#x5E94;&#x7528;&#x7684;&#x4E00;&#x4E2A; app &#x5B9E;&#x4F8B;&#xFF0C; &#x901A;&#x8FC7;&#x5B83;&#x6765;&#x8BBF;&#x95EE;&#x9700;&#x8981;&#x88AB;&#x6D4B;&#x8BD5;&#x7684; Controller&#x3001;Middleware&#x3001;Service &#x7B49;&#x5E94;&#x7528;&#x5C42;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// test/controller/home.test.js</span>
<span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);

describe(<span class="hljs-string">&apos;test/controller/home.test.js&apos;</span>, () =&gt; {

});
</code></pre>
<h4 id="t3111.3.5 ctx">11.3.5 ctx <a href="#t3111.3.5 ctx"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/controller/news.test.js&apos;</span>, () =&gt; {
  it(<span class="hljs-string">&apos;should get a ctx&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> ctx=app.mockContext({
          <span class="hljs-attr">session</span>: {
            <span class="hljs-attr">user</span>:{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>}
        }
    });
    assert(ctx.method === <span class="hljs-string">&apos;GET&apos;</span>);
    assert(ctx.url===<span class="hljs-string">&apos;/&apos;</span>);
    assert(ctx.session.user.name == <span class="hljs-string">&apos;zfpx&apos;</span>);
  });
});
</code></pre>
<h4 id="t3211.3.6 &#x6D4B;&#x8BD5;&#x6267;&#x884C;&#x987A;&#x5E8F;">11.3.6 &#x6D4B;&#x8BD5;&#x6267;&#x884C;&#x987A;&#x5E8F; <a href="#t3211.3.6 &#x6D4B;&#x8BD5;&#x6267;&#x884C;&#x987A;&#x5E8F;"> # </a></h4>
<p>&#x7279;&#x522B;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x6267;&#x884C;&#x987A;&#x5E8F;&#xFF0C;&#x5C3D;&#x91CF;&#x4FDD;&#x8BC1;&#x5728;&#x6267;&#x884C;&#x67D0;&#x4E2A;&#x7528;&#x4F8B;&#x7684;&#x65F6;&#x5019;&#x6267;&#x884C;&#x76F8;&#x5173;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-js">describe(<span class="hljs-string">&apos;egg test&apos;</span>, () =&gt; {
  before(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 1&apos;</span>));
  before(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 2&apos;</span>));
  after(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 6&apos;</span>));
  beforeEach(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 3&apos;</span>));
  afterEach(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 5&apos;</span>));
  it(<span class="hljs-string">&apos;should worker&apos;</span>, () =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;order 4&apos;</span>));
});
</code></pre>
<h4 id="t3311.3.7 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5;">11.3.7 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5; <a href="#t3311.3.7 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5;"> # </a></h4>
<p>egg-bin &#x652F;&#x6301;&#x6D4B;&#x8BD5;&#x5F02;&#x6B65;&#x8C03;&#x7528;&#xFF0C;&#x5B83;&#x652F;&#x6301;&#x591A;&#x79CD;&#x5199;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4F7F;&#x7528;&#x8FD4;&#x56DE; Promise &#x7684;&#x65B9;&#x5F0F;</span>
it(<span class="hljs-string">&apos;should redirect&apos;</span>, () =&gt; {
  <span class="hljs-keyword">return</span> app.httpRequest()
    .get(<span class="hljs-string">&apos;/&apos;</span>)
    .expect(<span class="hljs-number">302</span>);
});

<span class="hljs-comment">// &#x4F7F;&#x7528; callback &#x7684;&#x65B9;&#x5F0F;</span>
it(<span class="hljs-string">&apos;should redirect&apos;</span>, done =&gt; {
  app.httpRequest()
    .get(<span class="hljs-string">&apos;/&apos;</span>)
    .expect(<span class="hljs-number">302</span>, done);
});

<span class="hljs-comment">// &#x4F7F;&#x7528; async</span>
it(<span class="hljs-string">&apos;should redirect&apos;</span>, <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">await</span> app.httpRequest()
    .get(<span class="hljs-string">&apos;/&apos;</span>)
    .expect(<span class="hljs-number">302</span>);
});
</code></pre>
<h4 id="t3411.3.8 Controller &#x6D4B;&#x8BD5;">11.3.8 Controller &#x6D4B;&#x8BD5; <a href="#t3411.3.8 Controller &#x6D4B;&#x8BD5;"> # </a></h4>
<p><code>app.httpRequest()</code>&#x662F; <code>egg-mock</code> &#x5C01;&#x88C5;&#x7684; <a href="https://github.com/visionmedia/supertest">SuperTest</a> &#x8BF7;&#x6C42;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/controller/home.test.js&apos;</span>, () =&gt; {
  it(<span class="hljs-string">&apos;homeController&apos;</span>, <span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">await</span> app.httpRequest()
            .get(<span class="hljs-string">&apos;/&apos;</span>)
            .expect(<span class="hljs-number">200</span>)
            .expect(<span class="hljs-string">&apos;hello&apos;</span>);

            <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">await</span> app.httpRequest()
            .get(<span class="hljs-string">&apos;/&apos;</span>)
            .expect(<span class="hljs-number">200</span>)
                .expect(<span class="hljs-string">&apos;hello&apos;</span>);
           assert(result.status == <span class="hljs-number">200</span>);
  });
});
</code></pre>
<h4 id="t3511.3.9 post">11.3.9 post <a href="#t3511.3.9 post"> # </a></h4>
<pre><code class="lang-js">    router.post(<span class="hljs-string">&apos;/post&apos;</span>,controller.home.post);
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/controller/home.test.js&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;homeController&apos;</span>,<span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">let</span> user={<span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>};
        app.mockCsrf();
        <span class="hljs-keyword">await</span> app.httpRequest()
            .post(<span class="hljs-string">&apos;/post&apos;</span>)
            .type(<span class="hljs-string">&apos;form&apos;</span>)
            .send(user)
            .expect(<span class="hljs-number">200</span>)
            .expect(user);
  });
});
</code></pre>
<h4 id="t3611.3.10 service">11.3.10 service <a href="#t3611.3.10 service"> # </a></h4>
<p>Service &#x76F8;&#x5BF9;&#x4E8E; Controller &#x6765;&#x8BF4;&#xFF0C;&#x6D4B;&#x8BD5;&#x8D77;&#x6765;&#x4F1A;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#xFF0C; &#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; ctx&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7; ctx.service.${serviceName} &#x62FF;&#x5230; Service &#x5B9E;&#x4F8B;&#xFF0C; &#x7136;&#x540E;&#x8C03;&#x7528; Service &#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/service/news.test.js&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;newsService&apos;</span>,<span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">let</span> ctx = app.mockContext();
        <span class="hljs-keyword">let</span> result=<span class="hljs-keyword">await</span> ctx.service.news.list(<span class="hljs-number">1</span>,<span class="hljs-number">5</span>);
        assert(result.length == <span class="hljs-number">5</span>);
    });
});
</code></pre>
<h4 id="t3711.3.11 Extend &#x6D4B;&#x8BD5;">11.3.11 Extend &#x6D4B;&#x8BD5; <a href="#t3711.3.11 Extend &#x6D4B;&#x8BD5;"> # </a></h4>
<p>&#x5E94;&#x7528;&#x53EF;&#x4EE5;&#x5BF9; Application&#x3001;Request&#x3001;Response&#x3001;Context &#x548C; Helper &#x8FDB;&#x884C;&#x6269;&#x5C55;&#x3002; &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5BF9;&#x6269;&#x5C55;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;&#x9488;&#x5BF9;&#x6027;&#x7684;&#x7F16;&#x5199;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;</p>
<h5 id="t3811.3.11.1 application">11.3.11.1 application <a href="#t3811.3.11.1 application"> # </a></h5>
<p>egg-mock &#x521B;&#x5EFA; app &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5DF2;&#x7ECF;&#x5C06; Application &#x7684;&#x6269;&#x5C55;&#x81EA;&#x52A8;&#x52A0;&#x8F7D;&#x5230; app &#x5B9E;&#x4F8B;&#x4E86;&#xFF0C; &#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A; app &#x5B9E;&#x4F8B;&#x8BBF;&#x95EE;&#x6269;&#x5C55;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#x3002;</p>
<p>app/extend/application.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> cacheData={};
exports.cache={
    get(key) {
        <span class="hljs-keyword">return</span> cacheData[key];
    },
    set(key,val) {
        cacheData[key]=val;
    }
}
</code></pre>
<p>test/app/extend/cache.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/app/extend/cache.test.js&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;cache&apos;</span>,<span class="hljs-keyword">async</span> () =&gt; {
        app.cache.set(<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;zfpx&apos;</span>);
        assert(app.cache.get(<span class="hljs-string">&apos;name&apos;</span>) == <span class="hljs-string">&apos;zfpx&apos;</span>);
  });
});
</code></pre>
<h5 id="t3911.3.11.2 context">11.3.11.2 context <a href="#t3911.3.11.2 context"> # </a></h5>
<p>Context &#x6D4B;&#x8BD5;&#x53EA;&#x6BD4; Application &#x591A;&#x4E86;&#x4E00;&#x4E2A; app.mockContext() &#x6B65;&#x9AA4;&#x6765;&#x6A21;&#x62DF;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; Context &#x5BF9;&#x8C61;&#x3002;
app/extend/context.js</p>
<pre><code class="lang-js">exports.language=<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.get(<span class="hljs-string">&apos;accept-language&apos;</span>);
}
</code></pre>
<p>test/app/extend/context.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/app/extend/context.test.js&apos;</span>,() =&gt; {
    <span class="hljs-keyword">let</span> language=<span class="hljs-string">&quot;zh-cn&quot;</span>;
    it(<span class="hljs-string">&apos;cache&apos;</span>,<span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">const</span> ctx=app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&apos;Accept-Language&apos;</span>:language
            }
        });
        <span class="hljs-comment">//console.log(&apos;ctx.lan&apos;,ctx.lan())</span>
        assert(ctx.language() == language);
  });
});
</code></pre>
<h5 id="t4011.3.11.3 Request">11.3.11.3 Request <a href="#t4011.3.11.3 Request"> # </a></h5>
<p>&#x901A;&#x8FC7; ctx.request &#x6765;&#x8BBF;&#x95EE; Request &#x6269;&#x5C55;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x76F4;&#x63A5;&#x5373;&#x53EF;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports={
    get isChrome() {
        <span class="hljs-keyword">const</span> userAgent=<span class="hljs-keyword">this</span>.get(<span class="hljs-string">&apos;User-Agent&apos;</span>).toLowerCase();
        <span class="hljs-keyword">return</span> userAgent.includes(<span class="hljs-string">&apos;chrome&apos;</span>);
    }
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/app/extend/request.test.js&apos;</span>,() =&gt; {
    it(<span class="hljs-string">&apos;cache&apos;</span>,<span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">const</span> ctx=app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&apos;User-Agent&apos;</span>:<span class="hljs-string">&apos;I love Chrome&apos;</span>
            }
        });
        assert(ctx.request.isChrome);
  });
});
</code></pre>
<h5 id="t4111.3.11.4 response">11.3.11.4 response <a href="#t4111.3.11.4 response"> # </a></h5>
<p>Response &#x6D4B;&#x8BD5;&#x4E0E; Request &#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x3002; &#x901A;&#x8FC7; ctx.response &#x6765;&#x8BBF;&#x95EE; Response &#x6269;&#x5C55;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x76F4;&#x63A5;&#x5373;&#x53EF;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  get isSuccess() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.status === <span class="hljs-number">200</span>;
  },
};
</code></pre>
<pre><code class="lang-js">describe(<span class="hljs-string">&apos;isSuccess()&apos;</span>, () =&gt; {
  it(<span class="hljs-string">&apos;should true&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> ctx = app.mockContext();
    ctx.status = <span class="hljs-number">200</span>;
    assert(ctx.response.isSuccess === <span class="hljs-literal">true</span>);
  });

  it(<span class="hljs-string">&apos;should false&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> ctx = app.mockContext();
    ctx.status = <span class="hljs-number">404</span>;
    assert(ctx.response.isSuccess === <span class="hljs-literal">false</span>);
  });
});
</code></pre>
<h5 id="t4211.3.11.5 Helper">11.3.11.5 Helper <a href="#t4211.3.11.5 Helper"> # </a></h5>
<p>Helper &#x6D4B;&#x8BD5;&#x65B9;&#x5F0F;&#x4E0E; Service &#x7C7B;&#x4F3C;&#xFF0C;&#x4E5F;&#x662F;&#x901A;&#x8FC7; ctx &#x6765;&#x8BBF;&#x95EE;&#x5230; Helper&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528; Helper &#x65B9;&#x6CD5;&#x6D4B;&#x8BD5;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  money(val) {
    <span class="hljs-keyword">const</span> lang = <span class="hljs-keyword">this</span>.ctx.get(<span class="hljs-string">&apos;accept-language&apos;</span>);
    <span class="hljs-keyword">if</span> (lang.includes(<span class="hljs-string">&apos;zh-cn&apos;</span>)) {
      <span class="hljs-keyword">return</span> <span class="hljs-string">`&#xFFE5; <span class="hljs-subst">${val}</span>`</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">`$ <span class="hljs-subst">${val}</span>`</span>;
  },
};
</code></pre>
<pre><code class="lang-js">describe(<span class="hljs-string">&apos;money()&apos;</span>, () =&gt; {
  it(<span class="hljs-string">&apos;should RMB&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> ctx = app.mockContext({
      <span class="hljs-comment">// &#x6A21;&#x62DF; ctx &#x7684; headers</span>
      headers: {
        <span class="hljs-string">&apos;Accept-Language&apos;</span>: <span class="hljs-string">&apos;zh-CN,zh;q=0.5&apos;</span>,
      },
    });
    assert(ctx.helper.money(<span class="hljs-number">100</span>) === <span class="hljs-string">&apos;&#xFFE5; 100&apos;</span>);
  });

  it(<span class="hljs-string">&apos;should US Dolar&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> ctx = app.mockContext();
    assert(ctx.helper.money(<span class="hljs-number">100</span>) === <span class="hljs-string">&apos;$ 100&apos;</span>);
  });
});
</code></pre>
<h2 id="t43&#x53C2;&#x8003;">&#x53C2;&#x8003; <a href="#t43&#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://github.com/eggjs/egg-view-nunjucks">egg-view-nunjucks</a></li>
<li><a href="https://github.com/atian25/blog/issues/25">egg-debug</a></li>
<li><a href="https://www.easy-mock.com/docs">easy-mock</a></li>
<li><a href="https://github.com/nuysoft/Mock/wiki/Syntax-Specification">Mock</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
